package com.kpx.app.wigdet;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.kpx.app.R;
import com.kpx.app.base.ComAdapter;
import com.kpx.app.base.ComViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * 底部弹出Dialog
 *
 * @author Jack Lance
 */
public class BottomUpDialog extends BaseDialog implements AdapterView.OnItemClickListener, View.OnClickListener {

    private ListView listView;
    private String[] title;
    private OnPositionClickListener listener;
    private OnPositionClickListener2 listener2;
    private TextView btnCancel;

    public BottomUpDialog(Context context, String[] title) {
        super(context);
        this.title = title;
        setContentView();

    }

    public BottomUpDialog(Context context, String[] title, OnPositionClickListener listener) {
        super(context);
        this.title = title;
        this.listener = listener;
        setContentView();
    }

    public BottomUpDialog(Context context, String[] title, OnPositionClickListener2 listener2) {
        super(context);
        this.title = title;
        this.listener2 = listener2;
        setContentView();

    }

    public BottomUpDialog(Context context, int theme) {
        super(context, theme);
        this.title = title;
        setContentView();
    }

    protected BottomUpDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    private void setContentView() {
        setContentView(R.layout.bottom_up_dialog_layout);
        listView = (ListView) findViewById(R.id.listview);
        btnCancel = (TextView) findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(this);
        getWindow().setWindowAnimations(R.style.DialogAnimation);
        getWindow().setGravity(Gravity.BOTTOM);
        if (title.length > 5) {
            findViewById(R.id.balanceView).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.balanceView).setVisibility(View.GONE);
        }
        ArrayList<String> titless = new ArrayList<>();
        for (int i = 0; i < title.length; i++) {
            titless.add(title[i]);
        }
        ButtomUpListAdapter mAdapter = new ButtomUpListAdapter(getContext(), titless,R.layout.bottom_up_dlg_item_layout);
        listView.setAdapter(mAdapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnCancel:
                dismiss();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (listener != null && title != null) {
            listener.onPositionClick(position);
        }

        if (listener2 != null && title != null) {
            listener2.onPositionClick(position, this);
        }
    }

    public interface OnPositionClickListener {
        void onPositionClick(int position);
    }

    public interface OnPositionClickListener2 {
        void onPositionClick(int position, BottomUpDialog dialog);
    }

    class ButtomUpListAdapter extends ComAdapter<String> {

        /**
         * 构造方法
         *
         * @param context  上下文对象
         * @param datas    数据
         * @param layoutId
         * @version 1.0
         * @createTime 2015年5月24日, 上午11:47:30
         * @updateTime 2015年5月24日, 上午11:47:30
         * @createAuthor chencong
         * @updateAuthor chencong
         * @updateInfo (此处输入修改内容, 若无修改可不写.)
         */
        public ButtomUpListAdapter(Context context, List<String> datas, int layoutId) {
            super(context, datas, layoutId);
        }

        @Override
        public void convert(ComViewHolder holder, String s, int position) {
            holder.setText(R.id.title,s);
            if (getCount() == 1) {// 只有一条，四角都有圆角
                holder.getConvertVeiw().setBackgroundResource(R.drawable.bottom_up_item_selector);
            } else {
                if (position == 0) {// 第一条顶部左右圆角
                    holder.getConvertVeiw().setBackgroundResource(R.drawable.dlg_radius_top_left_right_selector);
                } else if (position < getCount() - 1) {// 中间没有圆角
                    holder.getConvertVeiw().setBackgroundResource(R.drawable.dlg_no_selector);
                } else {// 最后一条底部圆角
                    holder.getConvertVeiw().setBackgroundResource(R.drawable.dlg_radius_bottom_left_right_selector);
                }
            }
        }
    }
}
